package com.yst.sqlbuilder;

import com.yst.lambda.TableFieldFunc;

public class SQLAggField<T> implements IAggFieldExpression{

    private final TableFieldFunc<T,?> field;
    private final EnumAggregation enumAggregation;
    public final TableFieldFunc<T,?> getFuncField(){
        return field;
    }
    private IAliasExpression tableAlias;
    public IAliasExpression getTableAlias(){
        return this.tableAlias;
    }
    public SQLAggField<T> setTableAlias(IAliasExpression tableAlias){
        this.tableAlias=tableAlias;
        return this;
    }
    public SQLAggField(TableFieldFunc<T,?> field,EnumAggregation aggregation){
        this(null,field,aggregation);
    }
    public SQLAggField(IAliasExpression tableAlias, TableFieldFunc<T,?> field, EnumAggregation aggregation){
        this.tableAlias = tableAlias;
        this.field=field;
        this.enumAggregation=aggregation;
    }
    @Override
    public String generateExpression(GenerateParam param) {
        SQLField<T> sqlField=new SQLField<>(field,this.tableAlias);
        return enumAggregation.expression.concat("(").concat(sqlField.generateExpression(param))
                .concat(")");
    }
    @Override
    public TableFieldFunc<T,?> getField() {
        return field;
    }
    @Override
    public EnumAggregation getAggregation() {
        return enumAggregation;
    }
    public static <T> SQLAggField<T> count(TableFieldFunc<T,?> field){
        return new SQLAggField<>(field,EnumAggregation.COUNT);
    }
    public static <T> SQLAggField<T> max(TableFieldFunc<T,?> field){
        return new SQLAggField<>(field,EnumAggregation.MAX);
    }
    public static <T> SQLAggField<T> sum(TableFieldFunc<T,?> field){
        return new SQLAggField<>(field,EnumAggregation.SUM);
    }
    public static <T> SQLAggField<T> min(TableFieldFunc<T,?> field){
        return new SQLAggField<>(field,EnumAggregation.MIN);
    }
    public static <T> SQLAggField<T> avg(TableFieldFunc<T,?> field){
        return new SQLAggField<>(field,EnumAggregation.AVG);
    }



}
